package com.acme.bankapp.domain.account;

import java.io.Serializable;

import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account, Serializable{
	
	private double balance;
	
	public AbstractAccount(){
		this(0);
	}

	public AbstractAccount(double balance) {
		if(balance < 0)
			throw new IllegalArgumentException();
		this.balance = balance;
	}
	
	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public void deposit(double x){
		if(x<0)
			throw new IllegalArgumentException();
		this.balance += x;
	}
	
	public void withdraw(double x) throws NotEnoughFundsException{
		if(x<0)
			throw new IllegalArgumentException();
		
		if (this.balance < x)
			throw new NotEnoughFundsException("Not enough money for withdraw");
		this.balance -= x;
		
	}
	
	public double maximumAmountToWithdraw() {
		return getBalance();
	}
	
	public boolean isValidBalance(){
		boolean checksuccess = true;
		if(balance < 0){
			checksuccess = false;
		}
		return checksuccess;
	}
	
	public long decimalValue(){
		return Math.round(balance);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}
	
	


}

